package com.example.bailin.giftsay.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseActivity;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.UrlProvide;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by bailin on 16/8/31.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivLoginClose;
    private EditText etLoginUsername;
    private EditText etLoginPassword;
    private Button btnLogin;
    private TextView tvLogin;
    private SharedPreferences shared;

    @Override
    protected int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        ivLoginClose = bindView(R.id.iv_login_close);
        etLoginUsername = bindView(R.id.et_login_username);
        etLoginPassword = bindView(R.id.et_login_password);
        btnLogin = bindView(R.id.btn_login);
        tvLogin = bindView(R.id.tv_login);
        btnLogin.setOnClickListener(this);
        tvLogin.setOnClickListener(this);
        ivLoginClose.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        Bmob.initialize(this, "d56d6973b86cc9cf2a71774f4f16ccfe");
        shared = getSharedPreferences(UrlProvide.NAME, MODE_PRIVATE);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_login_close:
                finish();
                break;

            case R.id.btn_login:
                final String uN = handle(etLoginUsername.getText().toString());
                String pW = handle(etLoginPassword.getText().toString());
                if (uN.equals("")) {
                    Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                }else if (pW.equals("")) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else {
                    BmobUser bmobUser =  new BmobUser();
                    bmobUser.setUsername(uN);
                    bmobUser.setPassword(pW);

                    bmobUser.login(new SaveListener<BmobUser>() {
                        @Override
                        public void done(BmobUser bmobUser, BmobException e) {
                            if (e == null) {
                                SharedPreferences.Editor editor = shared.edit();
                                editor.putString("username", uN);
                                editor.apply();

                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                                Intent intent = new Intent();
                                intent.putExtra("username", uN);
                                setResult(RESULT_OK, intent);
                                finish();

                            } else {
                                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                                etLoginUsername.setText(null);
                                etLoginPassword.setText(null);
                            }
                        }
                    });
                }

                break;

            case R.id.tv_login:
                Intent intent = new Intent(MyApp.getContext(), RegisterActivity.class);
                startActivityForResult(intent, 101);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 101:
                if (resultCode == RESULT_OK) {
                    etLoginUsername.setText(data.getStringExtra("username"));
                    etLoginPassword.setText(data.getStringExtra("passwork"));
                }

                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private String handle(String content) {
        return content.replace(" ", "");
    }
}





















